/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui;

import com.logical.allhit.setting.Account;
import com.logical.allhit.setting.Setting;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author comp18
 */
public class AddAccount extends javax.swing.JDialog {

    private Account account;

    /**
     * Creates new form AddAccount
     */
    public AddAccount(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public AddAccount(Account account, Frame owner, boolean modal) {
        super(owner, modal);
        this.account = account;
        initComponents();
        nameTextField.setText(account.getName());
        uidTextField.setText(Long.toString(account.getUid()));
        userTypeComboBox.setSelectedIndex((account.getUsertype().equals("pro_user") ? 0 : 1));
        passwordField.setText(account.getPassword());
        trPasswordField.setText(account.getTrPassword());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        rePasswordField = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        trPasswordField = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        retrPasswordField = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        uidTextField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        userTypeComboBox = new javax.swing.JComboBox();
        cancelButton = new javax.swing.JButton();

        jScrollPane1.setViewportView(jTextPane1);

        jLabel2.setText("jLabel2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add New Account to System");

        jLabel1.setText("Name");

        nameTextField.setColumns(20);

        jLabel3.setText("Password");

        passwordField.setColumns(20);
        passwordField.setToolTipText("password of account");
        passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                passwordFieldKeyReleased(evt);
            }
        });

        jLabel4.setText("Re enter password");
        jLabel4.setToolTipText("");
        jLabel4.setEnabled(false);

        rePasswordField.setColumns(20);
        rePasswordField.setToolTipText("password of account for typeing error");
        rePasswordField.setEnabled(false);

        jLabel5.setText("Transaction Password");

        trPasswordField.setColumns(20);
        trPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                trPasswordFieldKeyReleased(evt);
            }
        });

        jLabel6.setText("Re enter Tr. password");
        jLabel6.setEnabled(false);

        retrPasswordField.setColumns(20);
        retrPasswordField.setEnabled(false);

        jLabel7.setText("UID");

        uidTextField.setColumns(20);

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        resetButton.setText("Reset");
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });

        jLabel8.setText("User Type");

        userTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "pro_user", "freeuser" }));

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel8)
                    .addComponent(saveButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(nameTextField)
                    .addComponent(uidTextField)
                    .addComponent(passwordField)
                    .addComponent(rePasswordField)
                    .addComponent(trPasswordField)
                    .addComponent(retrPasswordField)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(resetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(userTypeComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(47, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(userTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel7)
                    .addComponent(uidTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel3)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(rePasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel5)
                    .addComponent(trPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel6)
                    .addComponent(retrPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(resetButton)
                    .addComponent(cancelButton))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void passwordFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordFieldKeyReleased
        if (evt.getKeyCode() != KeyEvent.VK_TAB) {
            rePasswordField.setText("");
            rePasswordField.setEnabled(true);
        }
    }//GEN-LAST:event_passwordFieldKeyReleased

    private void trPasswordFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_trPasswordFieldKeyReleased
        if (evt.getKeyCode() != KeyEvent.VK_TAB) {
            retrPasswordField.setText("");
            retrPasswordField.setEnabled(true);
        }
    }//GEN-LAST:event_trPasswordFieldKeyReleased

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
        nameTextField.setText("");
        userTypeComboBox.setSelectedIndex(0);
        uidTextField.setText("");
        passwordField.setText("");
        rePasswordField.setText("");
        rePasswordField.setEnabled(false);
        trPasswordField.setText("");
        retrPasswordField.setText("");
        retrPasswordField.setEnabled(false);
    }//GEN-LAST:event_resetButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        try {
            if (!passwordField.getText()
                    .trim().equals(rePasswordField.getText().trim())) {
                JOptionPane.showMessageDialog(MainView.getInstance().getFram(), "password must be same ");
                return;
            }
            if (!trPasswordField.getText()
                    .trim().equals(retrPasswordField.getText().trim())) {
                JOptionPane.showMessageDialog(MainView.getInstance().getFram(), "Transaction password must be same ");
                return;
            }
            account.setName(nameTextField.getText());
            account.setUsertype(userTypeComboBox.getSelectedIndex() == 0 ? "pro_user" : "freeuser");
            account.setUid(Long.parseLong(uidTextField.getText()));
            account.setPassword(passwordField.getText());
            account.setTrPassword(trPasswordField.getText());
            this.setVisible(false);
            this.dispose();
        } catch (NumberFormatException ex) {
            try {
                JOptionPane.showMessageDialog(MainView.getInstance().getFram(), "Entry numaric UID in diu filed");
            } catch (Exception e) {
            }
        } catch (Exception e) {
            Logger.getLogger(AddAccount.class.getName()).log(Level.WARNING, e.getMessage(), e);
        }
    }//GEN-LAST:event_saveButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JPasswordField rePasswordField;
    private javax.swing.JButton resetButton;
    private javax.swing.JPasswordField retrPasswordField;
    private javax.swing.JButton saveButton;
    private javax.swing.JPasswordField trPasswordField;
    private javax.swing.JTextField uidTextField;
    private javax.swing.JComboBox userTypeComboBox;
    // End of variables declaration//GEN-END:variables
}
